﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Storage;
using Storage.EntityModels;
using System.Data.Linq;
using Stats.Objects;

namespace Stats
{
    /// <summary>
    /// This class contains the method to retrieve TripLOA general stats used to update daily entry and display stats graph
    /// </summary>
    class GeneralStatsModule
    {
        /// <summary>
        /// This method contains operations to retrieve TriploA general statistics and returns 
        /// a GeneralStats object
        /// </summary>
        /// <returns>GeneralStats object</returns>
        public static GeneralStats getGeneralStatsOperation()
        {
            double total_money_saved = 0;
            double total_fuel_saved = 0;
            double total_pollution_saved = 0;
            double total_km_done = 0;
            int total_passengers_carried = 0;
            int max_passenger_carried = 0;
            int trips_proposed_number = 0;
            int trips_completed_number = 0;
            double average_trips_km = 0;
            int total_passengers_carried_onWay = 0;
            int registered_users = 0;
            int missedAppointment_byDr = 0;
            int missedAppointment_byPsg = 0;

            int number_Days_statsReferred = 0;

            try
            {
                TripLoaDataContext db = Storage.StorageManager.Instance.Entities;

                total_money_saved = AuxMethods.getTotalMoneySaved();

                total_fuel_saved= AuxMethods.getTotalFuelSaved();

                total_pollution_saved = AuxMethods.getTotalEmissionsSaved();

                total_km_done = (double)
                        (from st in db.DailyStatistics
                         select st.TotalKilometers)
                         .Sum();

                total_passengers_carried =
                        (from st in db.DailyStatistics
                         select (int)st.TotalPassengers)
                         .Sum();

                max_passenger_carried =
                        (from st in db.DailyStatistics
                         select (int)st.TotalPassengers)
                         .Max();

                trips_proposed_number =
                        (from st in db.DailyStatistics
                         select (int)st.TotalDailyTripProposal)
                         .Sum();

                trips_completed_number =
                        (from st in db.DailyStatistics
                         select (int)st.DailyCompletedTrips)
                         .Sum();
                if(trips_completed_number==0){
                average_trips_km =0;
                }else{
                    average_trips_km = total_km_done/trips_completed_number;
                }
                total_passengers_carried_onWay =
                        (from st in db.DailyStatistics
                         select (int)st.PassengerCarriedAlongTheWay)
                         .Sum();

                registered_users = AuxMethods.getRegisteredUsersCountOperation();
                        
                missedAppointment_byDr=
                        (from st in db.DailyStatistics
                         select (int)st.MissedAppointmentsByDrivers)
                         .Sum();

                missedAppointment_byPsg=
                        (from st in db.DailyStatistics
                         select (int)st.MissedAppointmentsByPassengers)
                         .Sum();

                number_Days_statsReferred = db.DailyStatistics.Count();


            }
            catch (InvalidOperationException) // in case Statistics table is empty, an exception is raised
            {
                total_money_saved = 0;
                total_fuel_saved = 0;
                total_pollution_saved = 0;
                total_km_done = 0;
                total_passengers_carried = 0;
                max_passenger_carried = 0;
                trips_proposed_number = 0;
                trips_completed_number = 0;
                average_trips_km = 0;
                total_passengers_carried_onWay = 0;
                registered_users = 0;
                missedAppointment_byDr = 0;
                missedAppointment_byPsg = 0;

                number_Days_statsReferred = 0;
                //System.Console.WriteLine(exc.StackTrace);
            }

            catch (NullReferenceException)
            {
                total_money_saved = 0;
                total_fuel_saved = 0;
                total_pollution_saved = 0;
                total_km_done = 0;
                total_passengers_carried = 0;
                max_passenger_carried = 0;
                trips_proposed_number = 0;
                trips_completed_number = 0;
                average_trips_km = 0;
                total_passengers_carried_onWay = 0;
                registered_users = 0;
                missedAppointment_byDr = 0;
                missedAppointment_byPsg = 0;

                number_Days_statsReferred = 0;
                //System.Console.WriteLine(exc.StackTrace);
            }

            catch (TypeInitializationException) // in case connection fails 2
            {
                total_money_saved = 0;
                total_fuel_saved = 0;
                total_pollution_saved = 0;
                total_km_done = 0;
                total_passengers_carried = 0;
                max_passenger_carried = 0;
                trips_proposed_number = 0;
                trips_completed_number = 0;
                average_trips_km = 0;
                total_passengers_carried_onWay = 0;
                registered_users = 0;
                missedAppointment_byDr = 0;
                missedAppointment_byPsg = 0;

                number_Days_statsReferred = 0;
                //System.Console.WriteLine(exc.StackTrace);
            }

            /*catch (System.Data.MappingException exc)
            {
                total_money_saved = 0;
                total_fuel_saved = 0;
                total_pollution_saved = 0;
                total_km_done = 0;
                total_passengers_carried = 0;
                max_passenger_carried = 0;
                trips_proposed_number = 0;
                trips_completed_number = 0;
                average_trips_km = 0;
                total_passengers_carried_onWay = 0;
                registered_users = 0;
                missedAppointment_byDr = 0;
                missedAppointment_byPsg = 0;

                number_Days_statsReferred = 0;
                System.Console.WriteLine(exc.StackTrace);
            }*/


            GeneralStats genS = new GeneralStats(AuxMethods.TruncateTo3Decimals(total_money_saved),AuxMethods.TruncateTo3Decimals(total_fuel_saved), 
                                AuxMethods.TruncateTo3Decimals(total_pollution_saved),AuxMethods.TruncateTo3Decimals(total_km_done),total_passengers_carried,
                                max_passenger_carried,trips_proposed_number,trips_completed_number,AuxMethods.TruncateTo3Decimals(average_trips_km),
                                total_passengers_carried_onWay,registered_users,missedAppointment_byDr,missedAppointment_byPsg,number_Days_statsReferred);
            return genS;
        }


    }
}
